﻿using Microsoft.BizTalk.MessageBoxOM;
using Microsoft.XLANGs.RuntimeTypes;
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.Resources;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Transactions;

namespace Bizspoke.BizTalk.Tracking
{
    internal class ExceptionMessageProvider
    {
        public static string GetExceptionErrorMessage(Exception exc)
        {
            if (exc is XLANGsDebugException)
            {
                return GetXlangProxyExceptionErrorMessage((XLANGsDebugException) exc);
            }
            if (exc is RemotingException)
            {
                return (exc.Message + ". " + ReadErrorMessageFromResources("err_RuntimeRequestedServiceWasNotFound"));
            }
            if (exc is SqlException)
            {
                if (0xafc == ((SqlException) exc).Number)
                {
                    return (ReadErrorMessageFromResources("err_SqlErrorCorruptedOrWrongDatabase") + "\n" + exc.Message);
                }
                if (0x11 == ((SqlException) exc).Number)
                {
                    return (ReadErrorMessageFromResources("err_SqlErrorCouldNotConnect") + "\n" + exc.Message);
                }
                if (0xe5 == ((SqlException) exc).Number)
                {
                    return ReadErrorMessageFromResources("err_SqlPermissionDeniedError");
                }
                return (ReadErrorMessageFromResources("err_GenericSqlError") + "\n" + exc.Message);
            }
            if (exc is COMException)
            {
                if (-2147217865 == ((COMException) exc).ErrorCode)
                {
                    return (ReadErrorMessageFromResources("err_SqlErrorCorruptedOrWrongDatabase") + "\n" + exc.Message);
                }
                if (-2147467259 == ((COMException) exc).ErrorCode)
                {
                    return (ReadErrorMessageFromResources("err_HatInaccessibleDbGeneric") + "\n" + exc.Message);
                }
                if (-2147217911 == ((COMException) exc).ErrorCode)
                {
                    return ReadErrorMessageFromResources("err_SqlPermissionDeniedError");
                }
            }
            if (((exc is TransactionException) || ((exc is SqlException) && exc.Message.ToUpper().Contains("0X8004D020"))) || ((exc is COMException) && (-2147168246 == (exc as COMException).ErrorCode)))
            {
                return string.Format(ReadErrorMessageFromResources("IDS_ERR_DTC_MISCONFIGURED"), exc.Message);
            }
            if (exc is UnexpectedErrorException)
            {
                return ReadErrorMessageFromResources("err_MBoxOM_Error");
            }
            return exc.Message;
        }

        public static string GetTheRightDatabaseAccessErrorMessage(DatabaseKind databaseKind)
        {
            switch (databaseKind)
            {
                case DatabaseKind.TrackingDatabase:
                    return ReadErrorMessageFromResources("err_HatInaccessibleTrackingDb");

                case DatabaseKind.ManagementDatabase:
                    return ReadErrorMessageFromResources("err_HatInaccessibleMgmtDb");

                case DatabaseKind.AnyDatabase:
                    return ReadErrorMessageFromResources("err_HatInaccessibleDbGeneric");
            }
            return string.Empty;
        }

        public static string GetXlangProxyExceptionErrorMessage(XLANGsDebugException exception)
        {
            string name = exception.GetType().Name;
            string str2 = ReadErrorMessageFromResources(name);
            if ((str2 == null) || (str2.Length == 0))
            {
                str2 = string.Format(ReadErrorMessageFromResources("XLANGsDebugException"), name);
            }
            if (exception is PostMessageFailedException)
            {
                str2 = str2 + "\n" + ReadErrorMessageFromResources("err_RuntimeRequestedServiceWasNotFound");
            }
            return str2;
        }

        public static string ReadErrorMessageFromResources(string resName)
        {
            //ResourceManager manager = new ResourceManager(typeof(ExceptionMessageProvider));
            ResourceManager manager = new ResourceManager(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Tracking.ExceptionMessageProvider", System.Reflection.Assembly.GetExecutingAssembly());
            return manager.GetString(resName, CultureInfo.CurrentUICulture);
        }

        public enum DatabaseKind
        {
            TrackingDatabase,
            ManagementDatabase,
            AnyDatabase
        }
    }
}

